﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MaximPortal.Model;
using MaximServices.Model;
using MaximServices.Services;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace maxim_portal.Controllers
{
    /// <summary>
    /// 规则
    /// </summary>
    [EnableCors("AllowCors")]
    [Route("api/[controller]")]
    [ApiController]
    public class MottoRuleController : ControllerBase
    {
        private MottoRuleServices _mottoRuleServices;

        public MottoRuleController(MottoRuleServices mottoRuleServices)
        {
            _mottoRuleServices = mottoRuleServices;
        }

        // GET: api/MottoRule
        [HttpGet]
        public ResultModel<PageData<MottoRule>> Get([FromQuery]DateTime? startDate = null,[FromQuery]DateTime? endDate = null,
                [FromQuery]string ruleName = "",[FromQuery]int pageIndex = 1,[FromQuery]int pageSize = 10)
        {
            try
            {
                PageData<MottoRule> list = _mottoRuleServices.GetList(new MottoRule
                {
                    StartDate = startDate,
                    EndDate = endDate,
                    Rule_Name = ruleName,
                },pageIndex,pageSize);
                return BuildResult.Success<PageData<MottoRule>>(list);

            }
            catch (Exception e)
            {
                return BuildResult.Error<PageData<MottoRule>>(e.Message,null);
            }
        }

        // POST: api/MottoRule
        /// <summary>
        /// 保存规则
        /// </summary>
        [HttpPost]
        public ResultModel<CreateMottoRuleResultModel> Post([FromBody]AddMottoRuleModel rule)
        {
            if(!ModelState.IsValid)
            {
                return BuildResult.Error<CreateMottoRuleResultModel>(ModelState.Values.First().Errors[0].ErrorMessage);
            }
            //id不为空的时候认定为修改
            if(!string.IsNullOrWhiteSpace(rule.Id))
            {
                try
                {
                    return _mottoRuleServices.ModifyMottoRule(new MottoRule
                    {
                        Id = rule.Id,
                        Rule_Name = rule.Rule_Name,
                        Rule_Json = rule.Rule_Json,
                        Usedefault_Rule = rule.UseDefault_Rule,
                        StartDate = rule.StartDate,
                        EndDate = rule.EndDate,
                        Modify_Time = DateTime.Now
                    });
                }
                catch (Exception e)
                {
                    return BuildResult.Error<CreateMottoRuleResultModel>(e.Message);
                }
            }

            try
            {
                MottoRule model = new MottoRule
                {
                    StartDate = rule.StartDate,
                    EndDate = rule.EndDate,
                    Rule_Name = rule.Rule_Name,
                    Rule_Json = rule.Rule_Json,
                    Usedefault_Rule = rule.UseDefault_Rule,
                    Create_Time = DateTime.Now,
                    Modify_Time = DateTime.Now,
                    Sort_Order = 1
                };
                return _mottoRuleServices.CreateMottoRule(model);
            }
            catch (Exception e)
            {
                return BuildResult.Error<CreateMottoRuleResultModel>(e.Message);
            }

        }
        /// <summary>
        /// 删除规则
        /// </summary>
        /// <param name="id"></param>
        // DELETE: api/ApiWithActions/5
        [HttpDelete("{id}")]
        public ResultModel<CreateMottoRuleResultModel> Delete(long id)
        {
            try
            {
                return _mottoRuleServices.DeleteMottoRule(id);
            }
            catch (Exception e)
            {
                return BuildResult.Error<CreateMottoRuleResultModel>(e.Message);
            }
        }
    }
}
